const service = require("./jellyHomeService.js");
const utils = require("../../../utils/util");
const WxParse = require("../../../utils/component/wxParse/wxParse.js");
const utilService = require('../../../utils/utilService.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    shareMsg: {},
    homeInfo: {},
    balanceTxt: "", //竞赛结束提示
    codeStatus: 0, //开奖状态
    ruleShow: false, //规则弹框是否显示
    ruleCon: "", //规则内容
    mineShow: false, //我的密码是否显示
    recordShow: false, //密码记录是否显示
    passwordNum: 0, //用户拥密码数量
    passwordList: [], //我的密码列表
    recordList: [], //密码记录列表
    recordPage: 1, //密码记录页数
    isHttp: true, //密码记录是否能请求
    winNumShow: false, //开奖结果是否显示
    winInfo: {}, //开奖结果
    battleAwardsShow: false, //竞赛奖励弹框是否显示
    awardsInfo: {}, //竞赛奖励
    isAllShow: false //是否全部展开
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow: function(options) {
    var self = this;
    //授权
    self.authAlert = self.selectComponent("#authAlert");
    self.authAlert.bindCatch()
    getApp().loginPromise.then(() => {
      self.getShareMessage(15);
      self.loadData()
    });
  },

  //点击授权按钮回调
  authSuccess() {
    this.getShareMessage(15);
    this.loadData()
  },

  loadData:function(){
    var self = this;
    service.getHomeInfo(3, function (res) {
      if (res.execErrCode == 200 || res.execErrCode == 201) {
        res.execData.period.totalAmount = Number(
          res.execData.period.total_amount
        );
        var num =
          res.execData.period.coffer_count_min -
          res.execData.total_coffer_codes_count;
        var codeStatus = 0;
        if (res.execErrCode == 200) {
          codeStatus = 0;
        } else {
          if (res.execData.win_code == "") {
            codeStatus = 1;
          } else {
            codeStatus = 2;
            res.execData.winCode = res.execData.win_code.split("");
            res.execData.period.preBeginTime = utils.formatDateDot(
              res.execData.period.pre_begin_time
            );
          }
        }
        self.setData({
          homeInfo: res.execData,
          balanceTxt:
          num > 0
            ? "每场密码发放完毕时，竞赛结束"
            : "即将结算，请等待公布胜利密码",
          codeStatus: codeStatus
        });
      }
    });

    /*service.getWinInfo(3, function (res) {
      if (res.execErrCode == 200) {
        res.execData.winCode = res.execData.code.split("");
        self.setData({
          winInfo: res.execData
        });
        let option = { currentTarget: { dataset: { var: "winNumShow" } } };
        self.openPopups(option);
      }
    });*/
  },
  /**
   * 获取分享信息
   */
  getShareMessage: function (type) {
    var self = this;
    utilService.shareSettings(type,
      function (data) {
        self.setData({
          shareMsg: data
        })
      },
      function (error) {

      }
    );
  },
  /**
   * 打开弹框
   */
  openPopups: function(e) {
    let obj = {};
    obj[e.currentTarget.dataset.var] = true;
    this.setData(obj);
  },
  /**
   * 关闭弹框
   */
  closePopups: function(e) {
    let obj = {};
    obj[e.currentTarget.dataset.var] = false;
    this.setData(obj);
    this.data.isHttp = true;
    this.data.recordPage = 1;
  },
  /**
   * 打开规则弹框
   */
  openRule: function(e) {
    var self = this;
    service.getRule(3, res => {
      if (res.execErrCode == 200) {
          var nodeStr = res.execData.activity_rule;
        //   nodeStr = nodeStr.replace(/&nbsp;/g, '');
          nodeStr = nodeStr.replace(/&rdquo;/g, '');
          nodeStr = nodeStr.replace(/&ldquo;/g, '');
          this.setData({ruleCon: nodeStr})
        self.openPopups(e);
        // WxParse.wxParse("ruleCon", "html", res.execData.activity_rule, self);
      }
    });
  },
  /**
   * 打开我的密码弹框
   */
  openMine: function(e) {
    var self = this;
    if(self.data.codeStatus==0){
      service.getMyPassword(3, self.data.homeInfo.period.id, function(res) {
        if (res.execErrCode == 200) {
          self.setData({
            passwordList: res.execData.codes
          });
          self.openPopups(e);
        }
      });
    }else{

    }
  },
  /**
   * 打开密码记录弹框
   */
  openRecord: function(e, fn) {
    if(this.data.codeStatus==0){
        if (this.data.isHttp) {
          this.setData({
            isHttp: false
          });
          service.getPasswordRecord(3, this.data.recordPage, res => {
            if (res.execErrCode == 200) {
              this.setData({
                isHttp: true
              });
              res.execData.user_codes.filter(function(item) {
                item.createdAt = utils.formatDateDot(item.created_at);
                return item;
              });
              if (res.execData.user_codes.length < 15) {
                this.setData({
                  isHttp: false
                });
              }
              let recordList = "";
              if (this.data.recordPage == 1) {
                recordList = res.execData.user_codes;
                this.openPopups(e);
              } else {
                recordList = recordList.concat(res.execData.user_codes);
              }
              this.setData({
                recordList: recordList,
                passwordNum: res.execData.user_codes_count
              });
              this.data.recordPage++;
              if(fn){
                  fn();
              }
            }
          })
        }
    }
  },
  /**
   * 我的密码打开记录弹框
   */
  goOpenRecord: function(e) {
    let option = { currentTarget: { dataset: { var: "mineShow" } } };
    this.openRecord(e, () => {
        this.closePopups(option);
    });
  },
  /**
   * 密码记录打开我的密码弹框
   */
  goOpenMine: function(e) {
    let option = { currentTarget: { dataset: { var: "recordShow" } } };
    this.openRecord(e, () => {
        this.closePopups(option);
    });
  },
  /**
   * 跳转到我的奖品
   */
  goMyAwards: function() {
    wx.navigateTo({
      url: "../winRecord/winRecord"
    });
  },
  /**
   * 跳转到往期结果
   */
  goAwardsResult: function() {
    wx.navigateTo({
      url: "../passWordStar/passWord"
    });
  },
  /**
   * 跳转到提现
   */
  goTiXian: function() {
    wx.navigateTo({
      url: "../../../withdrawCash/pages/withdrawCash/withdrawCash"
    });
  },
  /**
   * 密码记录滚动
   */
  recordScroll: function() {
    this.openRecord();
  },
  /**
   * 打开竞赛奖励弹框
   */
  openBattleAwards: function(e) {
    var self = this;
    service.getBattleAwards(3, function(res) {
      if (res.execErrCode == 200) {
        self.setData({
          awardsInfo: res.execData
        });
        self.openPopups(e);
      }
    });
  },
  /**
   * 文字展开
   */
  openText: function() {
    this.setData({
      isAllShow: true
    });
  },
  /**
   * 文字合上
   */
  closeText: function() {
    this.setData({
      isAllShow: false
    });
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var title = this.data.shareMsg.title
    title = title.replace(/#/, getApp().globalData.userInfo.nickName)
    var param = '?userId=' + getApp().globalData.userId
    var path = 'pages/happyJelly/jellyHome/jellyHome' + param
    return {
      title: title,
      path: path,
      imageUrl: this.data.shareMsg.pic,
      success: function (res) {
        // 需要在页面onLoad()事件中实现接口
        wx.showShareMenu({
          // 要求小程序返回分享目标信息
          withShareTicket: true
        });
      },
      fail: function (res) {
      }
    }
  }
});
